package com.yiren.backstage.judge.services.standard;

import android.content.Context;

import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.entity.MonitorCountEntity;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;
import com.yiren.backstage.currency.enums.LogicConnectiveEnum;
import com.yiren.backstage.currency.enums.RelationalOperatorEnum;
import com.yiren.backstage.judge.api.IExamStateCallBack;
import com.yiren.backstage.judge.services.standard.impl.PackageStateServiceImpl;
import com.yiren.backstage.judge.thread.AutoJudgeThread;

import java.util.List;

/**
 * Time:2021/11/10 14:12
 * Author:xiaozan
 * Description:检测车辆状态是否符合
 */
public class CheckCarStateIsAccordService {

    private IPackageStateService iPackageStateService;
    private CarStateBean realCarState;  //实时车辆状态
    private IExamStateCallBack iExamStateCallBack;

    public CheckCarStateIsAccordService(Context context,IExamStateCallBack iExamStateCallBack) {
        this.iExamStateCallBack = iExamStateCallBack;
        iPackageStateService = new PackageStateServiceImpl(context);
        realCarState = CarStateBean.getInstance();
    }

    /**
     * 判断当前车辆状态是否符合
     *
     * @param startCarState        评判开始时初始车辆状态对象
     * @param monitorCountEntities
     * @return
     */
    public boolean isAccordCurrentCarState(AutoJudgeThread autoJudgeThread, CarStateBean startCarState, List<MonitorCountEntity> monitorCountEntities) {
        boolean countResult = true;
        if (null == realCarState) {
            return false;
        }
        try {
            for (MonitorCountEntity monitorCount : monitorCountEntities) {
                countResult = monitorCounter(autoJudgeThread, startCarState, countResult, monitorCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return countResult;
    }

    /**
     * 判断当前车辆状态是否符合
     *
     * @param startCarState
     * @param monitorCount
     * @return
     */
    public boolean monitorCounter(AutoJudgeThread autoJudgeThread, CarStateBean startCarState, boolean countResult, MonitorCountEntity monitorCount) {
        double targetParameterValue = 0;
        float monitorParameterValue = 0;
        float monitorParameterMinValue = 0;
        float monitorParameterMaxValue = 0;

        if (!monitorCount.getCarStateName().equals("")) {
            switch (CarStateNameEnum.valueOf(monitorCount.getCarStateName())) {
                case ROTATE_SPEED:   //转速
                    targetParameterValue = realCarState.getRotateSpeed();
                    break;
                case SPEED:          //车速
                    targetParameterValue = realCarState.getSpeed();
                    break;
                case PASSING_LIGHT:  // 近光
                    targetParameterValue = realCarState.getPassingLight().ordinal();
                    break;
                case DISTANCE_LIGHT: //远光
                    targetParameterValue = realCarState.getDistanceLight().ordinal();
                    break;
                case FOGLIGHT:       //雾灯
                    targetParameterValue = realCarState.getFoglight().ordinal();
                    break;
                case RIGHT_LIGHT:    //右转灯
                    targetParameterValue = realCarState.getRightLight().ordinal();
                    break;
                case LEFT_LIGHT:     //左转灯
                    targetParameterValue = realCarState.getLeftLight().ordinal();
                    break;
                case CAUTION_LIGHT:  //警示灯
                    targetParameterValue = realCarState.getCautionLight().ordinal();
                    break;
                case DRIVING_LAMP:   //行车灯
                    targetParameterValue = realCarState.getDrivingLamp().ordinal();
                    break;
                case PARKING_BRAKE:  //手刹
                    targetParameterValue = realCarState.getParkingBrake().ordinal();
                    break;
                case FOOT_BRAKE:     //脚刹
                    targetParameterValue = realCarState.getFootBrake().ordinal();
                    break;
                case CAR_DOOR:       //车门
                    targetParameterValue = realCarState.getCarDoor().ordinal();
                    break;
                case SAFETY_BELT:    //安全带
                    targetParameterValue = realCarState.getSafetyBelt().ordinal();
                    break;
                case HORN:           //喇叭
                    targetParameterValue = realCarState.getHorn().ordinal();
                    break;
                case CAR_KEY:        //钥匙
                    targetParameterValue = realCarState.getCarKey().ordinal();
                    break;
                case RAIN_WIPPER:
                    targetParameterValue = realCarState.getRainWipper().ordinal();
                    break;
                case GEAR:           //档位
                    targetParameterValue = realCarState.getGear().ordinal();
                    break;
                case SEPARATION_REUNION: //离合
                    targetParameterValue = realCarState.getSeparationReunion().ordinal();
                    break;
                case CAR_HEAD_LEFT:  //左前
                    targetParameterValue = realCarState.getCarHeadLeft().ordinal();
                    break;
                case CAR_TAIL_RIGHT: //右后
                    targetParameterValue = realCarState.getCarTailRight().ordinal();
                    break;
                case CAR_HEAD_RIGHT: //右前
                    targetParameterValue = realCarState.getCarHeadRight().ordinal();
                    break;
                case CAR_TAIL_LEFT:  //左后
                    targetParameterValue = realCarState.getCarTailLeft().ordinal();
                    break;
                case REARVIEW_MIRROR://后视镜
                    targetParameterValue = realCarState.getRearviewMirror().ordinal();
                    break;
                case SEAT:           //座椅
                    targetParameterValue = realCarState.getSeat().ordinal();
                    break;
                case REFLECTOR_MIRROR:       //反光镜
                    targetParameterValue = realCarState.getReflectorMirror().ordinal();
                    break;
                case FINGERPRINT_INSTRUMENT: //指纹仪
                    targetParameterValue = realCarState.getFingerprint().ordinal();
                    break;
                case JUDGE_EXECUTE_FINISH://项目是否执行完成
                    targetParameterValue = iPackageStateService.judgeExecuteFinish(autoJudgeThread).ordinal();
                    break;
                case FLAMEOUT://熄火
                    targetParameterValue = iPackageStateService.flameout().ordinal();
                    break;
                case DEFLECTION_ANGLE://偏转角度
                    targetParameterValue = iPackageStateService.deflectionAngle(startCarState.getNsbsAngleDiff(), realCarState.getNsbsAngleDiff(),1);
                    //直线行驶才回调
                    if(autoJudgeThread.getProjectEntity().getId().equals("40300")){
                        if(null != iExamStateCallBack) iExamStateCallBack.zxxsDeflectionAngle((int)targetParameterValue);
                    }
                    break;
                case LEFT_DEFLECTION_ANGLE://左偏转角度
                    targetParameterValue = iPackageStateService.deflectionAngle(startCarState.getNsbsAngleDiff(), realCarState.getNsbsAngleDiff(),2);
                    break;
                case RIGHT_DEFLECTION_ANGLE://右偏转角度
                    targetParameterValue = iPackageStateService.deflectionAngle(startCarState.getNsbsAngleDiff(), realCarState.getNsbsAngleDiff(),3);
                    break;
                case BRAKE_NO_STABLE:       //车辆制动不平顺检测
                    targetParameterValue = iPackageStateService.brakeNoStableCheck().ordinal();
                    break;
                case BY_PASS_GEAR:          //越级挂挡检测
                    targetParameterValue = iPackageStateService.bypassGearCheck().ordinal();
                    break;
                case QIBU_THROUGH_DYNAMIC:  //起步闯动
                    targetParameterValue = iPackageStateService.qibuThroughDynamic().ordinal();
                    break;
                case HURRY_OF_DIRECTION:    //急打方向
                    targetParameterValue = realCarState.getHurryOfDirection().ordinal();
                    break;
                /****************************高精度状态**********************************************/
                case PRESS_QB_KB_FULL_LINE: //压起步靠边实线
                    targetParameterValue = realCarState.getPressQbkbFullLine().ordinal();
                    break;
                case PRESS_S_LINE:   //压实线
                    targetParameterValue = realCarState.getPressSLine().ordinal();
                    break;
                case PRESS_X_LINE:   //压虚线
                    targetParameterValue = realCarState.getPressXLine().ordinal();
                    break;
                case PRESS_ZXQ:      //压中心圈
                    targetParameterValue = realCarState.getPressZxq().ordinal();
                    break;
                case TZX_CAR_PASS:          //压停止线检测
                    targetParameterValue = realCarState.getTzxCarPass().ordinal();
                    break;
                case RXHDX_CAR_PASS:        //压人行横道线检测
                    targetParameterValue = realCarState.getRxhdxCarPass().ordinal();
                    break;
                case ZXQ_RIGHT_TURN_CIRCLE: //中心圈右侧转弯
                    targetParameterValue = realCarState.getZxqRightTurnCircle().ordinal();
                    break;
                case RXHDX_TURN_ROUND:      //人行横道线掉头检测
                    targetParameterValue = realCarState.getRxhdxTurnRound().ordinal();
                    break;
                case CAR_INTERSECTS_SCQY:   //车辆与刹车区域相交检测
                    targetParameterValue = realCarState.getCarIntersectScqy().ordinal();
                    break;
                case CAR_TOP_PROHIBIT_REGION:  //将车辆停在人行横道、网状线内等禁止停车区域
                    targetParameterValue = realCarState.getCarTopProhibitRegion().ordinal();
                    break;
                case CAR_PRESS_XJJT:     //车压行驶箭头
                    targetParameterValue = realCarState.getCarPressXsjt().ordinal();
                    break;
                case LEFT_LANE_CHANGE:   //左变道
                    targetParameterValue = realCarState.getCarLeftChangeLane().ordinal();
                    break;
                case RIGHT_LANE_CHANGE:  //右变道
                    targetParameterValue = realCarState.getCarRightChangeLane().ordinal();
                    break;
                case BACK_SKATE_DISTANCE: //后溜距离
                    targetParameterValue = realCarState.getBackSkateDistance();
                    break;
                case DISTANCE_SIDE_LINE:  //偏转距离
                    targetParameterValue = iPackageStateService.deflectionDistance(startCarState.getRightFrontDistance(),realCarState.getRightBackDistance());
                    //直线行驶才回调
                    if(autoJudgeThread.getProjectEntity().getId().equals("40300") ){
                        if(null != iExamStateCallBack) iExamStateCallBack.zxxsDeflectionDistance((int)targetParameterValue);
                    }
                    break;
                case LEFT_FRONT_DISTANCE:
                    targetParameterValue = realCarState.getLeftFrontDistance();
                    break;
                case LEFT_BACK_DISTANCE:
                    targetParameterValue = realCarState.getLeftBackDistance();
                    break;
                case RIGHT_FRONT_DISTANCE:
                    targetParameterValue = realCarState.getRightFrontDistance();
                    break;
                case RIGHT_BACK_DISTANCE:
                    targetParameterValue = realCarState.getRightBackDistance();
                    break;
            }
            //获取监控计算参数
            switch (CarStateNameEnum.valueOf(monitorCount.getCarStateName())) {
                case SPEED:
                case ROTATE_SPEED:
                case DEFLECTION_ANGLE:
                case LEFT_DEFLECTION_ANGLE:
                case RIGHT_DEFLECTION_ANGLE:
                case BACK_SKATE_DISTANCE:
                case DISTANCE_SIDE_LINE:
                case LEFT_FRONT_DISTANCE:
                case LEFT_BACK_DISTANCE:
                case RIGHT_FRONT_DISTANCE:
                case RIGHT_BACK_DISTANCE:
                    monitorParameterValue = monitorCount.getValue() == null ? 0 : Float.parseFloat(monitorCount.getValue().toString());
                    monitorParameterMaxValue = monitorCount.getMaxValue() == null ? 0 : Float.parseFloat(monitorCount.getMaxValue().toString());
                    monitorParameterMinValue = monitorCount.getMinValue() == null ? 0 : Float.parseFloat(monitorCount.getMinValue().toString());
                    break;
                default:
                    monitorParameterValue = CarStateValueEnum.valueOf(monitorCount.getValue()).ordinal();
                    break;
            }
        }
        //逻辑连接词
        switch (LogicConnectiveEnum.valueOf(monitorCount.getLogicConnective())) {
            case AND:  //并且
                switch (RelationalOperatorEnum.valueOf(monitorCount.getRelationalOperator())) {
                    case GREATER_THAN: //大于
                        countResult = countResult && targetParameterValue > monitorParameterValue;
                        break;
                    case LESS_THAN:    //小于
                        countResult = countResult && targetParameterValue < monitorParameterValue;
                        break;
                    case EQUAL:        //等于
                        countResult = countResult && targetParameterValue == monitorParameterValue;
                        break;
                    case NOT_EQUAL:    //不等于
                        countResult = countResult && targetParameterValue != monitorParameterValue;
                        break;
                    case TO:           //到(计算范围)
                        countResult = countResult && (targetParameterValue >= monitorParameterMinValue && targetParameterValue <= monitorParameterMaxValue);
                        break;
                }
                break;
            case OR:  //或者
                switch (RelationalOperatorEnum.valueOf(monitorCount.getRelationalOperator())) {
                    case GREATER_THAN: //大于
                        countResult = countResult || targetParameterValue > monitorParameterValue;
                        break;
                    case LESS_THAN:    //小于
                        countResult = countResult || targetParameterValue < monitorParameterValue;
                        break;
                    case EQUAL:        //等于
                        countResult = countResult || targetParameterValue == monitorParameterValue;
                        break;
                    case NOT_EQUAL:    //不等于
                        countResult = countResult || targetParameterValue != monitorParameterValue;
                        break;
                    case TO:           //到(计算范围)
                        countResult = countResult || (targetParameterValue >= monitorParameterMinValue && targetParameterValue <= monitorParameterMaxValue);
                        break;
                }
                break;
        }
        return countResult;
    }
}
